home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7496 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to get a random strin
  5. Date: Tue, 27 Feb 96 00:04:01 GMT
  6. Organization: none
  7. Message-ID: <825379441snz@genesis.demon.co.uk>
  8. References: <4g19id$p7n@gail.ripco.com> <Dn5E0J.GKL@thinkage.on.ca> <4gqir9$d5r@airdmhor.gen.nz>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gqir9$d5r@airdmhor.gen.nz>
  15.            gumboot@airdmhor.gen.nz "Simon Hosie" writes:
  16.  
  17. >In article <4g19id$p7n@gail.ripco.com> mambuhl@ripco.com (Martin Ambuhl)
  18. > writes:
  19. >> chancl@nevada.edu (Clapton Chan) in <4fh5od$qq0@news.nevada.edu> asks:
  20. >> 
  21. >> [A poor practice follows]
  22. >> if you do not #include <time.h>, you need
  23. >>     srand((unsigned)time(NULL));
  24. >
  25. >Alan Bowler:
  26. >> Worse than poor.  time() returns a time_t which might not be an
  27. >> integer type.  The above code implicitly declares "time()" as returning
  28. >> int.  Adding the cast to unsigned will not change that fact that
  29. >> this implicit declaration means your code could be looking in the
  30. >> wrong place.  For example:  Suppose "time_t" is actually "double"
  31. >> and not "int", and that the machine uses has separate floating
  32. >> point and integer registers.  (x86, pdp-11, ibm/370).  Then the
  33. >> above code could well result in the same vale being passed to srand()
  34. >> every time the program is called, because time() set the floating
  35. >> point result register and the above code is looking at an integer
  36. >> result register which untouched by time().
  37. >
  38. >  I don't follow that.. if time returns a float then won't the result be
  39. >cast and truncated to an int?
  40.  
  41. Only if the compiler knows that time() returns float. The only way it
  42. can know is if there is a declaration of time() in scope (e.g. from
  43. including time.h). Without that the compiler must assume that time()
  44. returns int hence will generate the code required (whixh may be none) to
  45. convert int to unsigned, *not* float to unsigned.
  46.  
  47. With an appropriate declaration this will work if the truncated value is
  48. representable as an unsigned int. If not the result is undefined behaviour.
  49. On most systems floats can represent much larger values than ints.
  50.  
  51. While integer operations where the result is unsigned cannot overflow
  52. (and produce undefined behaviour) floating point -> unsigned conversions
  53. *can*.
  54.  
  55. >Is the following legal, by the way?
  56. >
  57. >        char Temp[sizeof(time_t) + sizeof(unsigned)];
  58. >
  59. >        time((time_t *)Temp);
  60. >        srand(*(unsigned *)Temp);
  61.  
  62. There is no guarantee that Temp is aligned suitably for either a time_t or
  63. an unsigned. You could overcome that using malloc.
  64.  
  65. There is no guarantee that time_t is as long as unsigned. This means that
  66. not all bytes of *(unsigned *)Temp may be set.
  67.  
  68. C doesn't guarantee that accessing an object via an unsigned lvalue will
  69. be aliased to accessing it via a time_t lvalue.
  70.  
  71. What you can do is something like:
  72.  
  73.      {
  74.          time_t timeval = time(NULL);
  75.          unsigned char *ptr = (unsigned char *)&timeval;
  76.          unsigned seed = 0;
  77.          int    i;
  78.  
  79.          for (i = 0; i < sizeof timeval; i++)
  80.               seed = (seed + ptr[i]) * N;
  81.  
  82.          srand(seed);
  83.      }
  84.  
  85. where N can be chosen to 'diffuse' small changes in timeval throughout
  86. the seed. It should be at least odd so that lower order bits are affected.
  87. Making it prime may or may not produce any benefit but it shouldn't do
  88. any harm.
  89.  
  90. -- 
  91. -----------------------------------------
  92. Lawrence Kirby | fred@genesis.demon.co.uk
  93. Wilts, England | 70734.126@compuserve.com
  94. -----------------------------------------
  95.